﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using Excel;
using UnityEngine;
using System.Data;
/// <summary>
/// 读取Excel表
/// </summary>
public class ReadExcelData : MonoBehaviour
{
    //public string ExcelPathName = "data.xlsx";
    public string ExcelPathName;

    void Start()
    {
        GameReadExcel(ExcelPathName);
    }

    /// <summary>
    /// 只读Excel方法
    /// </summary>
    /// <param name="ExcelPath"></param>
    /// <returns></returns>
    public void GameReadExcel(string ExcelPath)
    {
        FileStream stream = File.Open(Application.streamingAssetsPath+"/" + ExcelPath, FileMode.Open, FileAccess.Read);
        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
        DataSet result = excelReader.AsDataSet();

        int columns = result.Tables[0].Columns.Count;//获取列数
        int rows = result.Tables[0].Rows.Count;//获取行数


        //从第三行开始读
        for (int i = 2; i < rows; i++)
        {
            WeaponInfo WeaponInfo = new WeaponInfo();
            WeaponInfo.ID = int.Parse(result.Tables[0].Rows[i][0].ToString());
            WeaponInfo.Name = result.Tables[0].Rows[i][1].ToString();
            WeaponInfo.NameChina = result.Tables[0].Rows[i][2].ToString();
            WeaponInfo.TypeId = int.Parse(result.Tables[0].Rows[i][3].ToString());
            WeaponInfo.TypeName = result.Tables[0].Rows[i][4].ToString();
            WeaponInfo.Grade = result.Tables[0].Rows[i][5].ToString();
            WeaponInfo.IsAuto = int.Parse(result.Tables[0].Rows[i][6].ToString());
            WeaponInfo.BaseType = int.Parse(result.Tables[0].Rows[i][7].ToString());
            WeaponInfo.Speed = float.Parse(result.Tables[0].Rows[i][8].ToString());
            WeaponInfo.DurationTime = float.Parse(result.Tables[0].Rows[i][9].ToString());
            WeaponInfo.AttackRateTime = float.Parse(result.Tables[0].Rows[i][10].ToString());
            WeaponInfo.Damage = float.Parse(result.Tables[0].Rows[i][11].ToString());
            WeaponInfo.AttackDistance = float.Parse(result.Tables[0].Rows[i][12].ToString());
            WeaponInfo.Icon = result.Tables[0].Rows[i][13].ToString();
            WeaponInfo.Prefab_body = result.Tables[0].Rows[i][14].ToString();
            WeaponInfo.Prefab_explosion = result.Tables[0].Rows[i][15].ToString();
            WeaponInfo.MusicStart =result.Tables[0].Rows[i][16].ToString();
            WeaponInfo.MusicEnd =result.Tables[0].Rows[i][17].ToString();
            WeaponInfo.SpecOne = int.Parse(result.Tables[0].Rows[i][18].ToString());
            WeaponInfo.SpecTwo = int.Parse(result.Tables[0].Rows[i][19].ToString());
            WeaponInfo.SpecThree = int.Parse(result.Tables[0].Rows[i][20].ToString());
            WeaponInfo.Desc = result.Tables[0].Rows[i][21].ToString();
            DataLists.instance.weaponInfos.Add(WeaponInfo);
            //for (int j = 0; j < columns; j++)
            //{
            //    string nvalue = result.Tables[0].Rows[i][j].ToString();
            //    Debug.Log(nvalue);
            //}
        }

    }
}
